package cn.wolfcode.luowowo.acticle.service.impl;

import cn.wolfcode.luowowo.acticle.domain.*;
import cn.wolfcode.luowowo.acticle.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyMapper;
import cn.wolfcode.luowowo.acticle.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.acticle.query.StrategyDetailQueryObject;
import cn.wolfcode.luowowo.acticle.service.IDestinationService;
import cn.wolfcode.luowowo.acticle.service.IStrategyDetailService;
import cn.wolfcode.luowowo.acticle.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 攻略明细服务实现类
 */
@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    //查询所有攻略明细
    @Override
    public PageInfo query(StrategyDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrder());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //删除攻略明细
    @Override
    public void delete(Long id) {
        strategyDetailMapper.deleteByPrimaryKey(id);
    }
    //保存/更新
    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //dest_id
        Strategy strategy = strategyMapper.selectByPrimaryKey(strategyDetail.getStrategy().getId());
        Destination destination = strategy.getDest();
        strategyDetail.setDest(destination);
        //author_id
        strategyDetail.setAuthorId(1L);
        //summary: 1.后台阅读文章后提取摘要来保存  2.截取前200字作为摘要
        String content = strategyDetail.getStrategyContent().getContent();
        if(content != null && content.length() >= 200){
            strategyDetail.setSummary(content.substring(0, 200));
        }else{
            strategyDetail.setSummary(content);
        }
        //createTime sql中完成
        //isbroad
        boolean ret = destinationService.isBroad(destination.getId());
        strategyDetail.setIsabroad(ret);

        if(strategyDetail.getId() != null){
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
            //保存文章内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        }else{
            strategyDetailMapper.insert(strategyDetail);
            //保存文章内容
            StrategyContent strategyContent = strategyDetail.getStrategyContent();
            strategyContent.setId(strategyDetail.getId());
            strategyContentMapper.insert(strategyContent);
        }
        //tags
        //保存标签与明细的中间表关系前先删除旧关系
        strategyDetailMapper.deleteRelationForDetailAndTagRelation(strategyDetail.getId());
        if(tags != null && tags.length() > 0){
            String[] tag = tags.split(",");

            for (String t : tag) {
                //优化点: 能不能排除重复的标签
                List<String> tagsName = strategyTagMapper.selectAllTagName();
                if (tagsName.contains(t)){
                    //保存标签与明细的中间表关系
                    Long tagId = strategyTagMapper.selectByTagName(t);
                    strategyDetailMapper.insertRelationForDetailAndTagRelation(tagId, strategyDetail.getId());
                    continue;
                }

                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(t);
                strategyTag.setState(StrategyTag.STATE_NORMAL);
                //int maxSequence = strategyTagMapper.selectMaxSequence();
                //strategyTag.setSequence(maxSequence + 1); //序号自增
                strategyTagMapper.insert(strategyTag);
                //保存标签与明细的中间表关系
                strategyDetailMapper.insertRelationForDetailAndTagRelation(strategyTag.getId(), strategyDetail.getId());
            }
        }
    }

    @Override
    public void changeState(Long id, Integer state) {
        strategyDetailMapper.updateState(id, state);
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public String getTagsByDetailId(Long detailId) {
        return strategyDetailMapper.selectTagsByDetailId(detailId);
    }

    @Override
    public List<StrategyDetail> getDetailTop3ByDestId(Long destId) {
        return strategyDetailMapper.selectDetailTop3ByDestId(destId);
    }

    @Override
    public List<StrategyDetail> getDetailByStrategyId(Long strategyId) {
        return strategyDetailMapper.selectDetailByStrategyId(strategyId);
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public void updateStatisData(StrategyStatisVO vo) {
        StrategyDetail detail = new StrategyDetail();
        try {
            BeanUtils.copyProperties(detail, vo);
            detail.setId(vo.getStrategyId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        strategyDetailMapper.updateStatisData(detail);
    }
}
